package cn.lopr.im.android.utils;

import java.net.Proxy;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import okhttp3.Cookie;
import okhttp3.CookieJar;
import okhttp3.HttpUrl;

/**
 * @author shuangyang7
 * @date 2020/12/28 17:17
 */
public class OkHttpClient {

    private static class Get {
        private static final okhttp3.OkHttpClient OK_HTTP_CLIENT = new okhttp3.OkHttpClient()
                .newBuilder().proxy(Proxy.NO_PROXY).cookieJar(new CookieJar() {
                    private final Map<String, List<Cookie>> cookieStore = new HashMap<>();

                    @Override
                    public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {
                        cookieStore.put(url.host(), cookies);
                    }

                    @Override
                    public List<Cookie> loadForRequest(HttpUrl url) {
                        List<Cookie> cookies = cookieStore.get(url.host());
                        return cookies != null ? cookies : new ArrayList<>();
                    }
                }).writeTimeout(10, TimeUnit.SECONDS)
                .readTimeout(10, TimeUnit.SECONDS)
                .connectTimeout(10, TimeUnit.SECONDS)
                .build();
    }

    public static okhttp3.OkHttpClient getInstance() {
        return Get.OK_HTTP_CLIENT;
    }
}
